Import('env')

# TODO(mio): Create a custom function that generates the configuration

conf_file = env.FindFile('config.d.in', ['#source/app'])

contents = ''
with open(conf_file.get_path(), 'r') as conf:
    contents = conf.read()

version_split = env['PROJECT_VERSION'].split('.')
ver_major = version_split[0]
ver_minor = version_split[1]
ver_patch = version_split[2]

contents = contents.replace('@PROJECT_VERSION_MAJOR@', ver_major)
contents = contents.replace('@PROJECT_VERSION_MINOR@', ver_minor)
contents = contents.replace('@PROJECT_VERSION_PATCH@', ver_patch)

contents = contents.replace('@PROJECT_VERSION_STRING@', env['PROJECT_VERSION'])

with open('config.d', 'w') as conf:
    conf.write(contents)

files = env.Files(
    'app.d',
    'config.d',
    'logger.d',
    'util.d',
    'cmds/artist.d',
    'cmds/artwork.d',
    'cmds/bookmarked.d',
    'cmds/compact.d',
    'cmds/daily.d',
    'cmds/errors.d',
    'cmds/following.d',
    'cmds/novel.d',
    'cmds/package.d',
    'cmds/prune.d',
    env.vcs_tag(command=['git', 'rev-parse', '--short', 'HEAD'],
        input_file='vcs_tag.d.in', output_file='vcs_tag.d',
        fallback='release')
)

# FIXME(mio): This will appear, regardless of whether the config.d file exists or not.
env.Clean('source/app/config.o', 'config.d')
env.Clean('source/app/vcs_tag.o', 'vcs_tag.d')

Return('files')
